/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-03-08     obito0       first version
 * 2023-12-03     Meco Man     support nano version
 */

#include <board.h>
#include <rtthread.h>
#include <drv_gpio.h>
#include "dap_main.h"
#ifndef RT_USING_NANO
#include <rtdevice.h>
#endif /* RT_USING_NANO */

/* defined the LED0 pin: PB2 */
#define LED0_PIN    GET_PIN(B, 2)
void chry_dap_thread_entry(void *parameter)
{
    static rt_uint8_t led_state = PIN_LOW;
    rt_pin_write(LED0_PIN, led_state);
    led_state = (led_state == PIN_LOW) ? PIN_HIGH : PIN_LOW;
}


int main(void)
{
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
    // extern void cdc_acm_init(uint8_t busid, uintptr_t reg_base);
    // cdc_acm_init(0, (uintptr_t)USB_BASE);

    extern void uartx_preinit(void);
    uartx_preinit();
    chry_dap_init(0, (uintptr_t)USB_BASE);

    /*创建一个软件定时器, 用于闪烁LED0*/
    rt_timer_t led_timer = rt_timer_create("led_timer",
        chry_dap_thread_entry, RT_NULL, 1000, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
    rt_timer_start(led_timer);

    while (1)
    {
        chry_dap_handle();
        chry_dap_usb2uart_handle();

        // rt_pin_write(LED0_PIN, PIN_HIGH);
        // rt_thread_mdelay(500);
        // rt_pin_write(LED0_PIN, PIN_LOW);
        // rt_thread_mdelay(500);
    }
}
